import { _decorator, Component, Node, v3, MeshRenderer, Vec3, Tween, tween } from 'cc';
import { BasicComponet } from '../../../Init/Basic/BasicComponet';
import { GlobalConfig } from '../../../Init/Config/GlobalConfig';
import { EventTypes } from '../../../Init/Managers/EventTypes';
import { AudioEnum } from '../../../Init/SystemAudio/AudioEnum';
import { AudioSystem } from '../../../Init/SystemAudio/AudioSystem';
import GlobalPool from '../../../Init/Tools/GlobalPool';
const { ccclass, property } = _decorator;

@ccclass('Gold3dEffect')
export class Gold3dEffect extends BasicComponet {
    pos = v3();
    isFinish = false;
    @property(MeshRenderer)
    msr: MeshRenderer = null;
    a_transData = 'a_transData';
    transData = [0, 0, 0, 0];

    setData(p) {
        this.pos.set(p);
        AudioSystem.playEffect(AudioEnum.getGold);
        this.playAnim();
    }

    playAnim() {
        let offset = v3(8, 0, 0);
        let r = Math.random() * 6.28;
        Vec3.rotateY(offset, offset, Vec3.ZERO, r);
        Tween.stopAllByTarget(this.pos);
        this.isFinish = false;

        let t = 1;
        tween(this.pos).by(t, { x: offset.x, z: offset.z }).start();
        tween(this.pos).
            to(t / 2, { y: this.pos.y + 9 }, { easing: 'sineOut' }).
            to(t / 2, { y: this.pos.y + 1 }, { easing: 'sineIn' }).
            delay(2).
            call(() => {
                this.isFinish = true;
            }).start();
    }

    reset() {
        Tween.stopAllByTarget(this.pos);
    }

    rot = v3()
    update(dt) {
        this.rot.y += dt * 300;
        if (this.isFinish) {
            this.node.active = false;
            GlobalPool.put(this.node);
            this.emit(EventTypes.UIEvents.AddGoldEffect, { wpos: this.pos, gold: 0, isAnim: false });
        }
        this.node.setPosition(this.pos);
        this.node.eulerAngles = this.rot;
    }
}

